package mylibrary.concurrent;

import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class TestFutureTask {

	public static void main(String[] args) throws Exception {
		
		ExecutorService exec = Executors.newFixedThreadPool(5);
		Callable call = new Callable() {
			public String call() throws Exception{
				Thread.sleep(1000 * 5);
				return "Other less important but longtime things.";
			}
		};
		
		Runnable run = new Runnable() {
			public void run(){
				try {
					Thread.sleep(1000 * 5);
				} catch (InterruptedException e) {
				}
				System.out.println("Other less important but longtime things.");
			}
		};
		
		//Future task = exec.submit(call);
		Future task = exec.submit(run);
		System.out.println("1 " + new Date());
		
		Thread.sleep(1000 * 3);
		System.out.println("Let's do something important");
		System.out.println("2 " + new Date());
		task.cancel(true);
		
		if(!task.isCancelled()) {
			String obj = (String) task.get();
			System.out.println(obj);
		}
		System.out.println("3 " + new Date());
		
		exec.shutdown();
	}
}
